/*
 * === Try to implement an algorithm which would detect if a resource has been inactive for some period, then it would put it to some 
 * 		kind of a sleep mode, which would decrease its power consumption based on the level of its sleep.
 * 
 * 		It would also keep a check that when a sleeping resource gets a job, it changes it state, which would increase its base 
 * 		consumption considerably for a short period of time and then perform as normal.
 */

import java.util.HashMap;

public class ServerCluster implements IServerCluster
{
	private HashMap<Integer,IServer> serverMap = null;
	private int serverCount;
	private volatile Boolean running = null;
	
	public ServerCluster(Boolean running)
	{
		serverMap = new HashMap<Integer,IServer>();
		serverCount = 0;
		
		this.running = running;
	}
	
	public void addServer(Double pF,long server_memory, int npu, int pCores, String serverDesc)
	{
		int newServerKey = serverCount;
		
		if(!serverMap.containsKey(new Integer(newServerKey)))
		{
			IServer newServer = new Server(newServerKey,server_memory,pF,running,npu,pCores,serverDesc);
			serverMap.put(new Integer(newServerKey), newServer);
			
			serverCount++;
		}
	
		else
			System.out.println("Error, another resource with same key already exists !!");
	}
	
	public void removeServer(int key)
	{
		if(serverMap.containsKey(new Integer(key)))
		{
			serverMap.remove(new Integer(key));
			
			System.out.println("Resource with key: " + key + " removed successfuly");
		}
		
		else
			System.out.println("Error !! No resource with the given key: " + key + " exists.");
		
	}
	
	public int getServerCount()
	{
		return serverCount;
	}
	
	public IServer getServer(int key)
	{
		if(serverMap.containsKey(new Integer(key)))
		{
			return serverMap.get(new Integer(key));
		}
		
		else
			System.out.println("Error !! No resource with given key found !!");
		
		return null;
	}
}
